package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author 京平笙
 * @since 2021-12-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
	
    @Resource
    private UserMapper userMapper;
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public boolean exitThisUser(User user, HttpSession session) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>()
                .eq("uname", user.getUname())
                .eq("upassword", user.getUpassword())
                .eq("uis_delete",0);
        User tempUser = userMapper.loginSuccess(user, userQueryWrapper);
        if (tempUser!=null){
            session.setAttribute("user",tempUser);
//            session.setAttribute("uid",tempUser.getUid());
//            session.setAttribute("uname",tempUser.getUname());
            return true;
        }else {
            session.setAttribute("user",null);
            return false;
        }
    }

    @Override
    public boolean insertThisUser_Success(User user,HttpSession session) {
        if (null!=userMapper.selectOne(new QueryWrapper<User>().eq("uname",user.getUname()).eq("uis_delete",0))){
            if (1==userMapper.insert(user)){
                session.setAttribute("user", user);
                return true;
            }
        }
        return false;
    }
}
